
/**
 * @author 高岩
 * 
 * @version v1.0.0-20121230-RC
 * 
 * paging，用于控制分页，基于jquery编写，目前默认只是支持dhtmlx分页。属于离线计算分页类型：一次传入总数后，每次按照传入的总数进行计算得出分页。
 * 
 * －－－－－
 * 提供主要方法pagingDHXGrid，用户dhtmlx的grid分页，本方法只支持对grid.loadxml的方法数据支持，应提供完善的url数据加载链接。
 * 调用方法：
 * Paging.pagingDHXGrid({
 * 	pageSize : 20, //一个分页中总共显示的表格行数
 * 	limit : 20,//与pageSize相同参数，设置可忽略，或者设置成与pageSize一样的数值
 * 	start : 1,//设置显示的分页索引，默认设置第一页
 * 	containerId : 'pageArea',//本分页会自动生成分页工具栏，此为提供的div或者td等主键
 * 	grid : mygrid,//本方法是对dhtmlx的grid进行分页的，所以提供grid实体对象
 * 	countSize : 45,//是表格数据总共的数量
 * 	gridDoAfterRefresh:function(){???},//在处理gird时，当加载完成后处理的函数
 * 	url:'???'//加载数据的完整url
 * });
 * 
 * 在调用以上方法以后，会自动在url链接加入以下参数，用于action或者servlet等url处理类
 * pageSize//一个分页中总共显示的表格行数
 * start//分页索引
 * limit//和pageSize相同
 * currentPageStartIndex//本次分页页面开始的行数
 * currentPageEndIndex//本次分页页面结束的行数
 * 
 * －－－－－
 * 
 */
var Paging = {
	containerId : null
	,start : null
	,limit : null
	,countSize : null
	,pageSize : null
	,that : this
	,pagingDHXGrid : function(obj){
		var url	= obj.url;
		var grid = obj.grid;
		var gridDoAfterRefresh = obj.gridDoAfterRefresh;
		var reloadGrid = function(start,limit,pageSize){
			var appendPagingURL = "pageSize="+that.pageSize+"&start="+that.start+"&limit="+that.limit+"&currentPageStartIndex="+that.getCurrentPageStartIndex()+"&currentPageEndIndex="+that.getCurrentPageEndIndex();
			var u = (url+((url.indexOf("?")!=-1)?"&":"?")+appendPagingURL);
			if(gridDoAfterRefresh)grid.clearAndLoad(u,gridDoAfterRefresh);
			else grid.clearAndLoad(u);
		};
		this.init(obj,reloadGrid);
	}
	,init : function(obj,reload){
		this.setConfig(obj);
		that = this;
		var reloadGrid = function(){
			reload();
			that.updatePagingToobarHTML();
		};
		$("#"+that.containerId).html(
				"<table><tr><td>" +
							"<button  id='prev'>上一页</button>" +
							"<button id='next'>下一页</button>" +
							"<span id=\"pagingIndex\"></span>" +
							"转到 <input name=\"pagenav\" id=\"pagenav\" type=\"text\" size=\"2\"/> 页  <button id='go'>Go</button>&nbsp;&nbsp;" +
							"<span id='gridInfo'>Go</span>&nbsp;&nbsp;" +
							"显示 <input value=\""+that.pageSize+"\" name=\"pageSize\" id=\"pageSize\" type=\"text\" size=\"3\"/> 行 " +
							"<button  id='setupPageCount'>设置</button>" +
						"</td></tr></table>");
		$("#setupPageCount").bind("click",function(){
			if(!/^\d+$/.exec($("#pageSize").val())){
				return;
			}
			if($("#pageSize").val() <=0){
				alert("参数设置有误");
				return;
			}
			that.limit = $("#pageSize").val();
			that.start	= 1;
			that.pageSize = $("#pageSize").val();
			reloadGrid();
		});
		$("#prev").bind("click",function(){
			if(that.start==1){
				return
			}
			--that.start;
			reloadGrid(that);
		});
		$("#next").bind("click",function(){
			if(that.countSize % 2 == 0){
				if(that.countSize<=(that.pageSize*that.start)){
					return
				}
			}else{
				if(that.countSize<(that.pageSize*that.start)+1){//  李艳芹写(that.pageSize*that.start)+1原(that.pageSize*that.start)
					return
				}
			}
			++that.start;
			reloadGrid(that);
		});
		$('#go').bind("click",function(){
			var countPageSize_	= that.getCountGridSize();
			if(!/^\d+$/.exec($("#pagenav").val())){
				return;
			}
			if($("#pageSize").val() <=0){
				alert("参数设置有误");
				return;
			}
			if($("#pagenav").val()>countPageSize_){
				return;
			}
			that.start	= $("#pagenav").val();
			$("#pagenav").val('');
			reloadGrid();
		});
		reloadGrid(that);
	}
	,setConfig : function(obj){
		this.containerId = obj.containerId;
		this.start	= obj.start || 0;
		this.limit	= obj.limit || 0;;
		this.countSize = obj.countSize;
		this.pageSize = obj.pageSize || 20;
	}
	,getCountGridSize : function(){
		var countPageSize_	= 1;
		if(that.countSize/that.pageSize>1){
			if(that.countSize%that.pageSize==0){
				countPageSize_	= that.countSize/that.pageSize;
			}else{
				countPageSize_	= (that.countSize - (that.countSize%that.pageSize))/that.pageSize+1;
			}
		}
		return countPageSize_;
	}
	,getCurrentPageStartIndex : function(){
		return ((that.start-1)*that.pageSize+1);
	}
	,getCurrentPageEndIndex : function(){
		var gridIndex = that.start*that.pageSize;
		if(gridIndex>that.countSize) gridIndex	= that.countSize;
		return gridIndex;
	}
	,updatePagingToobarHTML : function(){
		var pagingToobarHTML = "显示"+that.getCurrentPageStartIndex()+"到"+that.getCurrentPageEndIndex()+"条，共计"+that.countSize+"条 当前第[ "+that.start+" ]页"+"，共[ "+that.getCountGridSize()+" ]页";
		$("#gridInfo").html(pagingToobarHTML);
	}

};